import { BasicColumn, FormSchema } from '@/components/Table'
import { getDictListByCode, getEnergyPriceType } from '@/api/sys/dict'
import { h } from 'vue'
import dayjs from 'dayjs'

// 全局能源类型字典映射
let energyTypeMap = {}
let energyTypeMap2 = {}
// 获取能源类型名称的工具函数
const getEnergyTypeName = (code) => {
  return energyTypeMap[code] || code
}
const getEnergyPriceTypeName = (code) => {
  return energyTypeMap2[code] || code
}
// 初始化能源类型字典
const initEnergyTypeDict = async () => {
  try {
    const result = await getDictListByCode('energy_type')
    energyTypeMap = result.reduce((map, item) => {
      map[item.itemCode] = item.itemName
      return map
    }, {})

    const results = await getEnergyPriceType()
    energyTypeMap2 = results.reduce((map, item) => {
      map[item.value] = item.key
      return map
    }, {})
  } catch (error) {
    console.error('获取能源类型字典失败:', error)
  }
}

// 在模块加载时初始化
initEnergyTypeDict()

export const searchFormSchema: FormSchema[] = [
  {
    field: 'dataTypeId',
    label: '能源类型',
    component: 'ApiSelect',
    defaultValue: '1770381347331145728',
    colProps: { span: 6 },
    componentProps: {
      api: getDictListByCode,
      params: 'energy_type',
      labelField: 'itemName',
      valueField: 'itemCode',
    },
  },
  {
    field: 'date',
    component: 'RangePicker',
    colProps: { span: 6 },
    componentProps: {
      placeholder: ['开始时间', '结束时间'],
      disabledDate: (current) => current && current > dayjs().endOf('day'),
    },
  },
]

export const columns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '能源类型',
    dataIndex: 'dataTypeId',
    customRender: ({ text }) => getEnergyTypeName(text),
  },
  {
    title: '开始日期',
    dataIndex: 'startDate',
    customRender: ({ text }) => dayjs(text).format('YYYY-MM-DD'),
  },
  {
    title: '结束日期',
    dataIndex: 'endDate',
    customRender: ({ text }) => dayjs(text).format('YYYY-MM-DD'),
  },
  {
    title: '计价方式',
    dataIndex: 'type',
    customRender: ({ text }) => getEnergyPriceTypeName(text),
  },
  {
    title: '固定价格',
    dataIndex: 'singlePrice',
    width: 140,
    customRender: ({ text }) => text || '--',
  },
  {
    title: '计价周期',
    dataIndex: 'period',
    customRender: ({ text }) => {
      const cycleMap = {
        DEFAULT: '每单位用量',
        HOUR: '每小时',
        DAY: '每天',
        MONTH: '每月',
        YEAR: '每年',
      }
      return cycleMap[text] || '--'
    },
  },
  {
    title: '描述',
    dataIndex: 'description',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
  },
]

export const addRecordFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    component: 'Input',
    rules: [{ required: true }],
  },
  {
    field: 'dataTypeId',
    label: '能源类型',
    component: 'ApiSelect',
    rules: [{ required: true }],
    componentProps: {
      api: getDictListByCode,
      params: 'energy_type',
      labelField: 'itemName',
      valueField: 'itemCode',
    },
  },
  {
    field: 'date',
    label: '起止时间',
    component: 'RangePicker',
    rules: [{ required: true }],
  },
  {
    field: 'type',
    label: '计价方式',
    component: 'ApiSelect',
    defaultValue: 1, // 新增：计价方式默认选第一个（value=1）
    rules: [{ required: true }],
    componentProps: {
      api: getEnergyPriceType,
      labelField: 'key',
      valueField: 'value',
    },
  },
  {
    field: 'singlePrice',
    label: '固定价格',
    component: 'InputNumber',
    rules: [{ required: true }],
  },
  {
    field: 'period',
    label: '计价周期',
    component: 'Select',
    defaultValue: 'DEFAULT',
    rules: [{ required: true }],
    componentProps: {
      options: [
        { label: '每单位用量', value: 'DEFAULT' },
        { label: '每小时', value: 'HOUR' },
        { label: '每天', value: 'DAY' },
        { label: '每月', value: 'MONTH' },
        { label: '每年', value: 'YEAR' },
      ],
    },
  },
  {
    field: 'description',
    label: '描述',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入',
      autoSize: { minRows: 2, maxRows: 3 },
    },
  },
]

export const editRecordFormSchema: FormSchema[] = [...addRecordFormSchema]